package com.lzb.browser.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.content.ComponentName;
import android.content.pm.PackageManager;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.blankj.utilcode.util.ToastUtils;
import com.lzb.browser.BaseAc;
import com.lzb.browser.Content;
import com.lzb.browser.R;
import com.lzb.browser.basic.utils.SPUtil;
import com.lzb.browser.databinding.ActivityCamouflageModeBinding;

public class CamouflageModeActivity extends BaseAc<ActivityCamouflageModeBinding> implements View.OnClickListener {

    private int type;

    @Override
    protected int onCreate() {
        return R.layout.activity_camouflage_mode;
    }

    @Override
    protected void initView() {
        mDataBinding.ivCamouflageModeBack.setOnClickListener(view -> getOnBackPressedDispatcher().onBackPressed());

        mDataBinding.llNormalMode.setOnClickListener(this);
        mDataBinding.llCalculatorMode.setOnClickListener(this);
        mDataBinding.llCompassMode.setOnClickListener(this);
        mDataBinding.tvChange.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.llNormalMode) {//正常模式
            type = 0;
            clearSelection();
            setSelection();
        } else if (id == R.id.llCalculatorMode) {//计算器模式
            type = 1;
            clearSelection();
            setSelection();
        } else if (id == R.id.llCompassMode) {//指南针模式
            type = 2;
            clearSelection();
            setSelection();
        } else if (id == R.id.tvChange) {//切换
            ToastUtils.showShort(getString(R.string.change_ing));
            SPUtil.putInt(mContext, Content.camouflageMode, type);
            changeAppIcon(type); // 修改应用图标
        }
    }

    @Override
    protected void initData() {
        type = SPUtil.getInt(mContext, Content.camouflageMode, 0);
        clearSelection();
        setSelection();
        // 同步应用图标到当前保存的模式
        changeAppIcon(type);
    }

    private void setSelection() {
        if (type == 0) {
            mDataBinding.llNormalMode.setBackgroundResource(R.drawable.camouflage_sel);
            mDataBinding.tvNormalMode.setTextColor(Color.parseColor("#BF96FF"));
        } else if (type == 1) {
            mDataBinding.llCalculatorMode.setBackgroundResource(R.drawable.camouflage_sel);
            mDataBinding.tvCalculatorMode.setTextColor(Color.parseColor("#BF96FF"));
        } else if (type == 2) {
            mDataBinding.llCompassMode.setBackgroundResource(R.drawable.camouflage_sel);
            mDataBinding.tvCompassMode.setTextColor(Color.parseColor("#BF96FF"));
        }
    }

    private void clearSelection() {
        mDataBinding.llNormalMode.setBackgroundResource(0);
        mDataBinding.tvNormalMode.setTextColor(Color.parseColor("#FFFFFF"));
        mDataBinding.llCalculatorMode.setBackgroundResource(0);
        mDataBinding.tvCalculatorMode.setTextColor(Color.parseColor("#FFFFFF"));
        mDataBinding.llCompassMode.setBackgroundResource(0);
        mDataBinding.tvCompassMode.setTextColor(Color.parseColor("#FFFFFF"));
    }

    /**
     * 根据伪装模式修改应用图标
     * @param mode 0-正常模式，1-计算器模式，2-指南针模式
     */
    private void changeAppIcon(int mode) {
        PackageManager pm = getPackageManager();
        String packageName = getPackageName();
        String targetComponent = packageName + (mode == 0 ? ".logo" : mode == 1 ? ".logo2" : ".logo3");
        
        // 检查目标组件是否已经启用
        ComponentName target = new ComponentName(this, targetComponent);
        int currentState = pm.getComponentEnabledSetting(target);
        
        // 只有在需要时才进行切换
        if (currentState != PackageManager.COMPONENT_ENABLED_STATE_ENABLED) {
            // 禁用所有图标组件
            disableAllIconComponents(pm);
            
            // 启用目标组件
            enableIconComponent(pm, targetComponent);
        }
    }
    
    /**
     * 禁用所有图标组件
     */
    private void disableAllIconComponents(PackageManager pm) {
        String packageName = getPackageName();
        String[] componentNames = {packageName + ".logo", packageName + ".logo2", packageName + ".logo3"};
        
        for (String componentName : componentNames) {
            try {
                ComponentName component = new ComponentName(this, componentName);
                // 获取当前组件状态，只有启用状态的才需要禁用
                int currentState = pm.getComponentEnabledSetting(component);
                if (currentState == PackageManager.COMPONENT_ENABLED_STATE_ENABLED) {
                    pm.setComponentEnabledSetting(
                        component,
                        PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                        PackageManager.DONT_KILL_APP
                    );
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    /**
     * 启用指定的图标组件
     */
    private void enableIconComponent(PackageManager pm, String componentName) {
        try {
            ComponentName component = new ComponentName(this, componentName);
            // 检查当前状态，避免重复启用
            int currentState = pm.getComponentEnabledSetting(component);
            if (currentState != PackageManager.COMPONENT_ENABLED_STATE_ENABLED) {
                pm.setComponentEnabledSetting(component,
                        PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                        PackageManager.DONT_KILL_APP);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}